﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace QunProject.Common
{
    /// <summary>
    /// 基本验证
    /// </summary>
    public static class Verify
    {
        /// <summary>
        /// 邮箱正则
        /// </summary>
        private static Regex regEmail = new Regex("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+");
        /// <summary>
        /// 电话号码正则
        /// </summary>
        private static Regex regPhone = new Regex("((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");
        /// <summary>
        /// IP地址正则
        /// </summary>
        private static Regex regIP = new Regex("d+.d+.d+.d+");
        /// <summary>
        /// 字母正则
        /// </summary>
        private static Regex regLetter = new Regex("^[A-Za-z]+$");
        /// <summary>
        /// 大写字母正则
        /// </summary>
        private static Regex regLetterUpper = new Regex("^[A-Z]+$");
        /// <summary>
        /// 小写字母正则
        /// </summary>
        private static Regex regLetterLower = new Regex("^[a-z]+$");
        /// <summary>
        /// 数字大小写字母正则
        /// </summary>
        private static Regex regNumberLetter = new Regex("^[A-Za-z0-9]+$");
        /// <summary>
        /// 数字正则
        /// </summary>
        private static Regex regNumber = new Regex("^[1-9]\\d*$");

        /// <summary>
        /// 验证一个字符串长度大于或等于一个较小长度和小于或等于一个较大值
        /// </summary>
        /// <param name="str">待验证的字符串</param>
        /// <param name="number1">较小长度</param>
        /// <param name="number2">较大长度</param>
        /// <returns></returns>
        public static bool VerifyLength(string str, int number1, int number2)
        {
            str = str == null ? "" : str;
            return str.Length <= number2 && str.Length >= number1;
        }
        /// <summary>
        /// 验证一个数字时候大于或等于一个较小值和小于或等于一个较大值
        /// </summary>
        /// <param name="number">待验证的数字</param>
        /// <param name="number1">较小值</param>
        /// <param name="number2">较大值</param>
        /// <returns></returns>
        public static bool NumberSize(int number, int number1, int number2)
        {
            return number >= number1 && number <= number2;
        }
        /// <summary>
        /// 是否是邮箱
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            str = str == null ? "" : str;
            return Verify.regEmail.IsMatch(str);
        }
        /// <summary>
        /// 是否是手机号码
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsPhone(string str)
        {
            str = str == null ? "" : str;
            return Verify.regPhone.IsMatch(str);
        }
        /// <summary>
        /// 是否是IP地址
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsIP(string str)
        {
            str = str == null ? "" : str;
            return Verify.regIP.IsMatch(str);
        }
        /// <summary>
        /// 是否全部为字母（包含大小写）
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsLetter(string str)
        {
            str = str == null ? "" : str;
            return Verify.regLetter.IsMatch(str);
        }
        /// <summary>
        /// 是否全部为字母（小写）
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsLetterLower(string str)
        {
            str = str == null ? "" : str;
            return Verify.regLetterLower.IsMatch(str);
        }
        /// <summary>
        /// 是否全部为字母（大写）
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsLetterUpper(string str)
        {
            str = str == null ? "" : str;
            return Verify.regLetterUpper.IsMatch(str);
        }
        /// <summary>
        /// 是否是字母和数字的组合
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsNumberLetter(string str)
        {
            str = str == null ? "" : str;
            return Verify.regNumberLetter.IsMatch(str);
        }
        /// <summary>
        /// 是否是数字
        /// </summary>
        /// <param name="str">待检测字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            str = str == null ? "" : str;
            return Verify.regNumber.IsMatch(str);
        }
    }
}
